package com.ruoyi.controller;

import java.net.URLDecoder;
import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import com.alibaba.fastjson2.JSONObject;
import com.jnxx.task.domain.OkrStaff;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.domain.TDailyStatement;
import com.ruoyi.domain.TDailyStatementAdmin;
import com.ruoyi.pojo.dto.SubDailyStatementDto;
import com.ruoyi.service.ITDailyStatementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 每日汇报Controller
 *
 * @author mzr
 * @date 2025-04-11
 */
@Slf4j
@RestController
@RequestMapping("/system/dailyStatement")
public class DailyStatementController extends BaseController
{
    @Autowired
    private ITDailyStatementService tDailyStatementService;

    /**
     * 查询每日汇报列表
     */
    @PreAuthorize("@ss.hasPermi('system:statement:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDailyStatement tDailyStatement)
    {
        startPage();
        List<TDailyStatement> list = tDailyStatementService.selectTDailyStatementList(tDailyStatement);
        return getDataTable(list);
    }

    /**
     * 导出每日汇报列表
     */
    @PreAuthorize("@ss.hasPermi('system:statement:export')")
    @Log(title = "每日汇报", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDailyStatement tDailyStatement)
    {
        List<TDailyStatement> list = tDailyStatementService.selectTDailyStatementList(tDailyStatement);
        ExcelUtil<TDailyStatement> util = new ExcelUtil<TDailyStatement>(TDailyStatement.class);
        util.exportExcel(response, list, "每日汇报数据");
    }

    /**
     * 获取每日汇报详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:statement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tDailyStatementService.selectTDailyStatementById(id));
    }

    /**
     * 修改每日汇报
     */
    @PreAuthorize("@ss.hasPermi('system:statement:edit')")
    @Log(title = "每日汇报", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDailyStatement tDailyStatement)
    {
        return toAjax(tDailyStatementService.updateTDailyStatement(tDailyStatement));
    }

    /**
     * 删除每日汇报
     */
    @PreAuthorize("@ss.hasPermi('system:statement:remove')")
    @Log(title = "每日汇报", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tDailyStatementService.deleteTDailyStatementByIds(ids));
    }


    /**
     * 提交每日汇报
     */
    @PreAuthorize("@ss.hasPermi('system:statement:add')")
    @Log(title = "每日汇报", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated SubDailyStatementDto body, HttpServletRequest request)
    {
        String token = request.getHeader("token");
        if (token==null){
            throw new GlobalException("未获取到当前用户的授权信息");
        }
        Long userId = tokenAnalyze(token);
        log.info("当前用户:"+userId);
        return tDailyStatementService.subDailyStatement(body,userId);
    }

    //用户查询每日汇报




    private Long tokenAnalyze(String token){
        try {
            String decode = URLDecoder.decode(token, "UTF-8");
            OkrStaff staff = JSONObject.parseObject(decode, OkrStaff.class);
            return staff.getId();
        }catch (Exception e) {
            log.error("解析token异常，token:"+token);
            throw new GlobalException("系统异常，用户身份解析失败！");
        }

    }

}
